package com.vxdata.utils.modules.hbase.service.impl;


import com.vxdata.common.core.domain.R;
import com.vxdata.utils.modules.hbase.util.HBaseUtil;
import com.vxdata.utils.modules.hbase.service.BaseService;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @description: HBase基本操作实现类
 * @author: lijj
 * @create: 2020-04-03 12:59
 **/
@Service
public class BaseServiceImpl implements BaseService {

    @Override
    public R createTable(String tableName, List<String> cfs) {
        if (HBaseUtil.createTable(tableName, cfs)) {
            return R.ok("表创建成功");
        } else {
            return R.fail("表创建失败");
        }
    }

    public R deleteTable(String tableName) {
        if (HBaseUtil.deleteTable(tableName)) {
            return R.ok("表删除成功");
        } else {
            return R.fail("表删除失败");
        }
    }

    @Override
    public R insertData(String tableName, String deviceTime, String modelID, String deviceID, Map<String, Object> dataMap) {
//        String rowKey = tableName + LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        String rowKey = modelID + deviceID + deviceTime;
        List<Put> puts = new ArrayList<>();
        Put put = new Put(Bytes.toBytes(rowKey));
        dataMap.forEach((key, value) -> {
            put.addColumn(Bytes.toBytes("data"), Bytes.toBytes(key),
                    Bytes.toBytes("" + value));
            puts.add(put);
        });
        try {
            HBaseUtil.putRows(tableName, puts);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok("插入成功");
    }


    @Override
    public R insertData(String tableName, List<Put> puts) {
        try {
            HBaseUtil.putRows(tableName, puts);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok("插入成功");
    }

    @Override
    public R isTableExist(String tableName) {
        Boolean result = HBaseUtil.isTableExist(tableName);
        if (result) {
            return R.fail("该表已存在");
        } else {
            return R.ok("该表不存在");
        }
    }


    public static void main(String[] args) {
        String taleName = "sun_标注";
        BaseServiceImpl service = new BaseServiceImpl();
//        // TODO 删除表
//        service.deleteTable(taleName);
//
//        // TODO 创建表
//        List<String> cfs = new ArrayList<>();
//        for (int j = 1; j < 3; j++) {
//            cfs.add("族" + j);
//        }
//        R pageTest = service.createTable(taleName, cfs);

        // TODO 插入数据
//        List<Put> putList = new ArrayList<>();
//        for (int i = 1; i < 100; i++) {
//            Put put = new Put(Bytes.toBytes("行" + i));
//            for (int j = 1; j < 3; j++) {
//                for (int k = 1; k < 6; k++) {
//                    String cfName = "族" + j;
//                    String qualifier = "列" + k;
//                    String data = cfName + qualifier;
//                    put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data));//增加列
//                    putList.add(put);
//                }
//            }
//        }
//        service.insertData(taleName, putList);


        // TODO 插入标记数据
        String data = "姓名:赵长林,身份证号：342423199005130590，职业：货车司机,货车(皖N21241）私家车(苏EBE632）,手机号：18516092520，居住地址：苏州市吴江区盛泽镇目澜社区绿杨2村28-405，发现途径：集中隔离点姓名:吴在理,身份证号：371324199804276436，职业：有机缘菜场27-28号摊位摊贩,手机号：18763892337，居住地址：苏州市姑苏区平江街道新弄堂14号1幢105室，发现途径：集中隔离点姓名:宋军平,身份证号：342623196906155039，职业：八珍烤禽食品公司配货员，货车车牌号：沪FNL612，私家车车牌号：苏U5Q800,手机号：18321803358，居住地址：张家港市杨舍镇泗港殷家埭35号旁私房内，发现途径：集中隔离点";
        List<Put> putList = new ArrayList<>();
        Put put = new Put(Bytes.toBytes("行2"));
        put.addColumn(Bytes.toBytes("族2"), Bytes.toBytes("列2"), Bytes.toBytes(data));//增加列
        putList.add(put);
        service.insertData(taleName, putList);
    }
}